package com.credit.mola.app.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.credit.mola.app.R
import com.credit.mola.app.app.App
import com.credit.mola.app.databinding.DialogVersionUpdateBinding
import com.credit.mola.app.databinding.DialogVersionUpdateItemBinding
import com.credit.mola.app.pro.ProDialog
import com.credit.mola.app.utils.DensityUtils
import com.credit.mola.app.utils.event.EventType
import com.credit.mola.app.utils.helper.CustomConfigHelper
import com.credit.mola.app.utils.http.HttpHelper

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 版本更新提示dialog
 */
class VersionUpdateDialog: ProDialog {
    private lateinit var binding: DialogVersionUpdateBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    override fun initWindow() {
        val params = window?.attributes
        params?.width = App.app.getPhoneWidth() - DensityUtils.dp2px(48f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogVersionUpdateBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        val versionData = CustomConfigHelper.getCmVersionObject()
        versionData?.let {
            binding.textVersionName.text = "V${it.optString("versionName")}"
            //添加更新描述
            addVersionUpdateInfoItem(it.optString("versionRemark"))

            //是否强制更新
            val isMustUpdate = CustomConfigHelper.isCmVersionUpdateMust()
            if (isMustUpdate){
                binding.btnCancel.visibility = View.GONE
                setCancelable(false)
                setCanceledOnTouchOutside(false)
            } else {
                binding.btnCancel.visibility = View.VISIBLE
                setCancelable(true)
                setCanceledOnTouchOutside(true)
            }
        }
    }

    override fun initListener() {
        setOnClickListener(R.id.btnUpdate)
        setOnClickListener(R.id.btnCancel)
    }

    override fun initData() {
    }


    /**
     * 添加更新描述
     */
    private fun addVersionUpdateInfoItem(info: String){
        binding.versionInfoLayout.removeAllViews()
        if (!info.contains("$")){
            val itemBinding = DialogVersionUpdateItemBinding.inflate(layoutInflater)
            itemBinding.textVersionInfo.text = info
            binding.versionInfoLayout.addView(itemBinding.root)
            return
        }
        val list = info.split("$")
        list.forEach {
            val itemBinding = DialogVersionUpdateItemBinding.inflate(layoutInflater)
            itemBinding.textVersionInfo.text = it
            binding.versionInfoLayout.addView(itemBinding.root)
        }
    }

    override fun onViewClick(v: View?) {
        when(v?.id){
            R.id.btnCancel -> {
                HttpHelper.addEvent(EventType.CLICK.CLICK_VERSION_UPDATE_CLOSE,eventRemark)
            }
            R.id.btnUpdate -> {
                HttpHelper.addEvent(EventType.CLICK.CLICK_VERSION_UPDATE_NOW,eventRemark)
                mCallback?.onUpdateNowClick()
            }
        }
        dismiss()
    }

    interface IDialogCallback {
        //按钮点击
        fun onUpdateNowClick()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_VERSION_UPDATE_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_VERSION_UPDATE_DISMISS,eventRemark)
    }
}